import { pull } from "langchain/hub";
import { ChatOpenAI } from "@langchain/openai";
import { weatherTool, weekdayTool } from "./tools.js";
import { createReactAgent, AgentExecutor } from "langchain/agents";
import "dotenv/config";

// 提示词
const prompt = await pull("hwchase17/react");

// 模型
const llm = new ChatOpenAI({
  model: "gpt-4o-mini",
  temperature: 0,
});

// 工具
const tools = [weatherTool, weekdayTool];

const agent = await createReactAgent({
  llm,
  tools,
  prompt,
});

const executor = new AgentExecutor({
  agent,
  tools,
  returnIntermediateSteps: true, // 开启中间步骤，方便观察 agent 运作的轨迹
  maxIterations: 5,
  handleParsingErrors: true, // 观察错误
});

// 回头用户的问题是扔给 executor
const res = await executor.invoke({
  input: "先查一下北京的天气，然后告诉我今天是星期几，最后用一句话总结。",
});
console.log("【最终输出】", res);
console.log("【intermediateSteps】", res.intermediateSteps);
